package com.km.noh.weatherinfo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.km.noh.weatherinfo.async.OnDataSyncCompleteListener;
import com.km.noh.weatherinfo.async.WeatherDataAsync;
import com.km.noh.weatherinfo.data.WeatherInfo;
import com.km.noh.weatherinfo.util.KMDialog;
import com.km.noh.weatherinfo.util.Util;

public class WeatherInfoActivity extends Activity implements OnDataSyncCompleteListener{
    /** Called when the activity is first created. */
	public static HashMap<String, Object> weatherData = new HashMap<String, Object>();
	public ArrayList<WeatherInfo> forecastList = new ArrayList<WeatherInfo>();
	private TextView vCity;
	private TextView vCondition;
	private TextView vTemp;
	private ListView vList;
	private WeatherAdapter adapter;
	private LayoutInflater inflate;
	private TextView vUpdateDate;
	private TextView vHumidity;
	private TextView vWindCondition;
	private ImageView vIcon;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        vIcon = (ImageView)findViewById(R.id.c_weather_icon);
        vCity = (TextView)findViewById(R.id.c_weather_city);
        vCondition = (TextView)findViewById(R.id.c_weather_condition);
        vTemp = (TextView)findViewById(R.id.c_weather_temp);
        vUpdateDate = (TextView)findViewById(R.id.c_weather_update);
        vHumidity = (TextView)findViewById(R.id.c_weather_humidity);
        vWindCondition = (TextView)findViewById(R.id.c_weather_wind_condition);
        vList = (ListView)findViewById(R.id.week_weather_list);
        inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        adapter = new WeatherAdapter();
        vList.setAdapter(adapter);
        new WeatherDataAsync(this).execute();
    }   
    
	@Override
	public void onSyncComplete(int resultCode) {
		forecastList = (ArrayList<WeatherInfo>)weatherData.get(WeatherInfo.TAG_FORECAST_CONDITIONS);
		setCurrentData();
		adapter.notifyDataSetChanged();
	}

	private void setCurrentData(){
		vIcon.setImageBitmap( Util.getBitmapImage(WeatherDataAsync.googleDomain + weatherData.get(WeatherInfo.TAG_ICON).toString() ,false));
		vCity.setText(weatherData.get(WeatherInfo.TAG_CITY).toString());
		vTemp.setText(weatherData.get(WeatherInfo.TAG_TEMP_C).toString()+"℃");
		vCondition.setText(weatherData.get(WeatherInfo.TAG_CONDITION).toString());		
		vHumidity.setText(weatherData.get(WeatherInfo.TAG_HUMIDITY).toString());		
		vWindCondition.setText(weatherData.get(WeatherInfo.TAG_WIND_CONDITION).toString());		
		vUpdateDate.setText("Update Date : "+weatherData.get(WeatherInfo.TAG_FORECAST_DATE).toString());		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("도시선택");
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		KMDialog.showSelectCity(this);
		return super.onOptionsItemSelected(item);
	}
	
	class WeatherAdapter extends BaseAdapter{

		private ImageView vWeekIcon;
		private TextView vWeekDate;
		private TextView vWeekCondition;
		private TextView vWeekTemp;

		@Override
		public int getCount() {
			return forecastList==null?0:forecastList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LinearLayout v;
			if(convertView==null)
				v = (LinearLayout)inflate.inflate(R.layout.weather_list_row, null);
			else
				v = (LinearLayout)convertView;
			
			WeatherInfo wInfo = forecastList.get(position);
			vWeekIcon = (ImageView)v.findViewById(R.id.week_weather_icon);
			vWeekDate = (TextView)v.findViewById(R.id.week_weather_date);
			vWeekCondition = (TextView)v.findViewById(R.id.week_weather_condition);
			vWeekTemp = (TextView)v.findViewById(R.id.week_weather_temp);
			vWeekIcon.setImageBitmap(Util.getBitmapImage(wInfo.getIconUrl(), false));
			vWeekDate.setText(wInfo.getDayOfWeek());
			vWeekCondition.setText(wInfo.getCondition());
			vWeekTemp.setText(wInfo.getTempHigh() +" / "+wInfo.getTempLow() +" ℃");
			
			return v;
		}
		
	}
}